/*
 * 创建用户：mleo
 * 项目名称：demo
 * 模块名称：demo
 * 文件名称：App
 * 修改时间：2017/11/14 13:55:27
 * Copyright (c) 2017.
 */

package com.ichochy.http;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.Serializable;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Hello world!
 */
public class App implements Serializable {
    private static final long serialVersionUID = 1L;
    static List<AreaInfo> areaInfos = new ArrayList<>();
    static List<BankNoInfo> bankInfos = new ArrayList<>();
    static String cookie = "PHPSESSID=l25qciniru8qarrdgdfa63j712";

    static String INFO_URL = "http://lianhanghao.com/index.php/Index/index/bank/%s/province/%s/city/%s/p/%s.html";

    public static void main(String[] args) throws Exception {
        areaInfos = JSON.parseArray(HTMLStatic.area_info,AreaInfo.class);
        System.out.println(JSON.toJSONString(areaInfos));

        Map<String,String> cityMaps = new HashMap<>();
        cityMaps.put("1","北京市");
        cityMaps.put("2","天津市");
        cityMaps.put("3","河北省");
        cityMaps.put("4","山西省");
        cityMaps.put("5","内蒙古自治区");
        cityMaps.put("6","辽宁省");
        cityMaps.put("7","吉林省");
        cityMaps.put("8","黑龙江省");
        cityMaps.put("9","上海市");
        cityMaps.put("10","江苏省");
        cityMaps.put("11","浙江省");
        cityMaps.put("12","安徽省");
        cityMaps.put("13","福建省");
        cityMaps.put("14","江西省");
        cityMaps.put("15","山东省");
        cityMaps.put("16","河南省");
        cityMaps.put("17","湖北省");
        cityMaps.put("18","湖南省");
        cityMaps.put("19","广东省");
        cityMaps.put("20","广西壮族自治区");
        cityMaps.put("21","海南省");
        cityMaps.put("22","四川省");
        cityMaps.put("23","重庆市");
        cityMaps.put("24","贵州省");
        cityMaps.put("25","云南省");
        cityMaps.put("26","西藏自治区");
        cityMaps.put("27","陕西省");
        cityMaps.put("28","甘肃省");
        cityMaps.put("29","青海省");
        cityMaps.put("30","宁夏回族自治区");
        cityMaps.put("31","新疆维吾尔族自治区");
        cityMaps.put("32","台湾");
        cityMaps.put("33","香港");
        cityMaps.put("34","澳门");


        for (AreaInfo info:areaInfos) {
            StringBuilder builder = new StringBuilder();
            builder.append("insert into bank_area_info (pid,province,cid,city) values (")
                    .append("'")
                    .append(info.getPid())
                    .append("',")
                    .append("'")
                    .append(cityMaps.get(info.getPid()))
                    .append("',")
                    .append("'")
                    .append(info.getSid())
                    .append("',")
                    .append("'")
                    .append(info.getName())
                    .append("');");
            System.out.println(builder.toString());
        }

//        String[] bankTypeIds = {"17","11","10","69","13","14","9"};
//        String[] bankTypeIds = {"1","2","3","4"};
//        for (String bankTypeId: bankTypeIds) {
//            getBankInfo(bankTypeId);
//        }
    }


    public static void getBankInfo(String bankTypeId) throws Exception {
        for (AreaInfo areaInfo: areaInfos) {
            int i = 1;
            while (i>0){
                String url = String.format(INFO_URL, bankTypeId,areaInfo.getPid(), areaInfo.getSid(), i);
                String html = httpPostHtml(url);
                String table = getTableInfo(html);
                if(null == table){
                    i = -1;
                }else{
                    i++;
                    List<BankNoInfo> infos = getTableDetailInfo(table);
                    editSQL(infos,bankTypeId,areaInfo.getPid(),areaInfo.getSid());
                }

            }
        }
    }


    public static void editSQL(List<BankNoInfo> infos,String bankTypeId,String pid,String sid) throws Exception {
        for (BankNoInfo info: infos) {
            StringBuilder sql = new StringBuilder();
            sql.append("insert into bank_no_info (id,bank_type_id,pid,sid,bank_name,bank_phone,bank_address)")
                    .append("values (")
                    .append("'")
                    .append(info.getId())
                    .append("',")
                    .append("'")
                    .append(bankTypeId)
                    .append("',")
                    .append("'")
                    .append(pid)
                    .append("',")
                    .append("'")
                    .append(sid)
                    .append("',")
                    .append("'")
                    .append(info.getName())
                    .append("',")
                    .append("'")
                    .append(info.getPhoneNo())
                    .append("',")
                    .append("'")
                    .append(info.getAddress())
                    .append("'); ");
            writeSQL(sql.toString(), bankTypeId);
        }
    }



    public static void writeSQL(String sql,String bankTypeId) throws Exception {
        FileWriter file = new FileWriter("bank_"+bankTypeId+".sql",true);
        BufferedWriter writer = new BufferedWriter(file);
        writer.write(sql);
        writer.newLine();
        writer.close();
        file.close();
    }


    public static List<BankNoInfo> getTableDetailInfo(String table) {
        List<BankNoInfo> infos = new ArrayList<>(10);
        String[] trs = table.split("</tr>");
        for (String tr : trs) {
            tr = tr.replace("<tr>", "").trim();
            if ("".equals(tr)) {
                continue;
            }
            BankNoInfo info = getDetailInfo(tr);
            infos.add(info);
        }
        return infos;
    }


    public static BankNoInfo getDetailInfo(String tr) {
        BankNoInfo info = new BankNoInfo();
        String[] tds = tr.split("</td>");
        String id = tds[0].replace(" ", "").replaceAll("<td\\S*>", "").trim();
        String name = tds[1].replace(" ", "").replaceAll("<td\\S*>", "").trim();
        String phoneNo = tds[2].replace(" ", "").replaceAll("<td\\S*>", "").trim();
        String address = tds[3].replace(" ", "").replaceAll("<td\\S*>", "").trim();
        info.setId(id);
        info.setName(name);
        info.setAddress(address);
        info.setPhoneNo(phoneNo);
        return info;
    }


    /**
     * 获取表格信息
     *
     * @param html
     * @return
     */
    public static String getTableInfo(String html) {
        Pattern pattern = Pattern.compile("(<tbody>)((\\s+)(\\S+\\s+)+)(</tbody>)");
        Matcher matcher = pattern.matcher(html);
        if (matcher.find()) {
            String content = matcher.group(2);
            return content;
        }
        return null;
    }

    /**
     * 获取所有的地区信息
     *
     * @throws Exception
     */
    public static void getAllArea() throws Exception {
        Integer count = 34;
        for (int i = 1; i <= count; i++) {
            String url = "http://www.lianhanghao.com/index.php/Index/Ajax?id=" + i;
            getAllArea(url);
        }
    }


    /**
     * 获取地区信息
     *
     * @param url
     * @throws Exception
     */
    public static void getAllArea(String url) throws Exception {
        String result = httpPostHtml(url);
        List<AreaInfo> infos = JSON.parseObject(result, new TypeReference<List<AreaInfo>>() {
        });
        areaInfos.addAll(infos);
    }


    /**
     * 获取页面信息
     *
     * @param url
     * @return
     * @throws Exception
     */
    public static String httpPostHtml(String url) throws Exception {
        String result = null;
        int echo_num = 3;
        do {
            try {
                System.out.println("请求地址：" + url);
                CloseableHttpClient client = HttpClients.createDefault();
                HttpPost http = new HttpPost(url);
                http.setHeader("Cookie", cookie);
                http.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0");
                CloseableHttpResponse response = client.execute(http);
                HttpEntity entity = response.getEntity();
                result = EntityUtils.toString(entity, "UTF-8");
                echo_num = 0;
            }catch (Exception e){
                echo_num --;
                e.printStackTrace();
            }
        }while (echo_num > 0);
        return result;
    }


}
